package com.amani.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 添加图片水印的服务类
 * */
public class WaterMark {
	
	/**
	 * @param sizeContext添加水印文字
	 * @param request
	 *            请求流对象
	 * @param request
	 *            响应流对象
	 * */
	@SuppressWarnings("deprecation")
	public static void createMarkSize(String sizeContext,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String path = request.getRealPath(request.getServletPath());
			FileInputStream in = new FileInputStream(path);
			Image src = ImageIO.read(in);
			int w = src.getWidth(null);
			int h = src.getHeight(null);
			BufferedImage img = new BufferedImage(w, h,
					BufferedImage.TYPE_INT_RGB);// 构建画板
			Graphics g = img.getGraphics();// 得到画笔
			g.drawImage(src, 0, 0, w, h, null);// 把源图片写入画板
			g.setColor(Color.red);
			g.drawString(sizeContext, 10, 5); // 添加文字
			g.dispose();// 生成图片
			JPEGImageEncoder e = JPEGCodec.createJPEGEncoder(response
					.getOutputStream());
			e.encode(img);
			response.getOutputStream().close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ImageFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param localPath
	 *            添加水印LOGO路径
	 * @param request
	 *            请求流对象
	 * @param request
	 *            响应流对象
	 **/
	@SuppressWarnings("deprecation")
	public static void createMarkLogo(String localPath,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			FileInputStream file = new FileInputStream(localPath);
			Image fimg = ImageIO.read(file);
			int fw = fimg.getWidth(null);
			int fh = fimg.getHeight(null);
			String path = request.getRealPath(request.getServletPath());
			FileInputStream in = new FileInputStream(path);
			Image src = ImageIO.read(in);
			int w = src.getWidth(null);
			int h = src.getHeight(null);
			BufferedImage img = new BufferedImage(w, h,
					BufferedImage.TYPE_INT_RGB);// 构建画板
			Graphics g = img.getGraphics();// 得到画笔
			g.drawImage(src, 0, 0, w, h, null);// 把原图片写入画板
			g.drawImage(fimg, w - 20, h - 15, fw, fh, null);// 把水印图片写入画板
			g.dispose();// 生成图片
			JPEGImageEncoder e = JPEGCodec.createJPEGEncoder(response
					.getOutputStream());
			e.encode(img);
			response.getOutputStream().close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ImageFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param localPath
	 *            添加水印图片路径
	 * @param request
	 *            请求流对象
	 * @param request
	 *            响应流对象
	 * @param width
	 *            水印图片的宽度
	 * @param height
	 *            水印图片的长度
	 **/
	@SuppressWarnings("deprecation")
	public static void createMarkLogo(String localPath,
			HttpServletRequest request, HttpServletResponse response,
			int width, int height) {
		try {
			FileInputStream file = new FileInputStream(localPath);
			Image fimg = ImageIO.read(file);
			int fw = fimg.getWidth(null);
			int fh = fimg.getHeight(null);
			String path = request.getRealPath(request.getServletPath());
			FileInputStream in = new FileInputStream(path);
			Image src = ImageIO.read(in);
			int w = src.getWidth(null);// w为你过滤图片的宽度
			int h = src.getHeight(null);// h为你过滤图片的长度
			BufferedImage img = new BufferedImage(w + width, h + height,
					BufferedImage.TYPE_INT_RGB);// 构建画板(画板的宽度为两个图片之和)
			Graphics g = img.getGraphics();// 得到画笔
			g.drawImage(src, 0, 0, w, h, null);// 把原图片写入画板
			g.drawImage(fimg, width, height, fw, fh, null);// 把水印图片写入画板
			g.dispose();// 生成图片
			JPEGImageEncoder e = JPEGCodec.createJPEGEncoder(response
					.getOutputStream());
			e.encode(img);
			response.getOutputStream().close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ImageFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}